package com.zizy.healthexpert.Database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class MyDataBase_T_FOODS {
	static final int PLACETOTAL = 115;// use for check if the table is
										// completely created
	static MyDataBase_T_FOODS.DataBase myDataBaseHelper;
	private SQLiteDatabase db;// the order is name, type,latitude,longtitude,
	final static String TABLENAME = "T_FOODS";
	
	public MyDataBase_T_FOODS(Context context) {
		myDataBaseHelper = new MyDataBase_T_FOODS.DataBase(context);
		this.db = myDataBaseHelper.getWritableDatabase();
		//initMyDataBase(db);
	}

	public void createTable(String tableName) {    
		String sql = "create table if not exists "
				+ tableName
				+ " (RecNo integer primary key autoincrement, _ID varchar(20) not null ,NAME varchar(20) not null, FOOD_TYPE INTEGER(60) not null,MEASURE_UNIT TEXT(60) not null,MEASURE_UNITAGE INTEGER(10)not null,LIST_SEQ TEXT(10),DETAIL TEXT(10) );";
		db.execSQL(sql);
	}	
	
	// init database
	public void initMyDataBase(SQLiteDatabase db) {
		int placeTotal = 0;
		this.createTable(TABLENAME);
		if (this.SearchT_FOODS("*") != null)
			placeTotal = this.SearchT_FOODS("*").size();
		// if is completely create then return
		if (placeTotal == MyDataBase_T_FOODS.PLACETOTAL)
			return;
		// or insert
		//myDataBaseHelper.deleteTable("");
		this.createTable(TABLENAME);
		this.Insert(1,1,"小麥",1,"克",100,null,null);
		
	}

	public void deleteTabel() {

	}

	 //insert operating
	public void Insert(int RecNo, int _ID, String NAME, int FOOD_TYPE,String MEASURE_UNIT ,int MEASURE_UNITAGE ,String LIST_SEQ ,String DETAIL ) {
		// the order is :name, type,longtitude,latitude
		
		this.db.execSQL(String
				.format("insert into %s (RecNo,_ID, NAME,FOOD_TYPE, MEASURE_UNIT , MEASURE_UNITAGE , LIST_SEQ , DETAIL) values('%s','%s','%s','%s','%s','%s','%s','%s')",
						TABLENAME, RecNo, _ID, NAME, FOOD_TYPE, MEASURE_UNIT , MEASURE_UNITAGE , LIST_SEQ , DETAIL));
	}
	
	// update operation
	public void Update(String field, Object value, String where_expression) {
		this.db.execSQL(String.format("update %s set password=%s where %s ;",
				TABLENAME, field, value, where_expression));
	}

	// Delete operation
	public void Delete(String where_expression) {
		this.db.execSQL(String.format("delete from %s  where %s ;", TABLENAME,
				where_expression));
	}

	public ArrayList<MyDataBase_T_FOODS.T_FOODS> SearchT_FOODS(String selectWhat) {
		ArrayList<MyDataBase_T_FOODS.T_FOODS> FOODSList = new ArrayList<MyDataBase_T_FOODS.T_FOODS>();
		Cursor cursor = db.rawQuery("select " + selectWhat + " from "
				+ TABLENAME, null);
		while (cursor.moveToNext()) {
			int _ID = cursor.getInt(0);
			String NAME = cursor.getString(1);
			int FOOD_TYPE = cursor.getInt(2);
			String MEASURE_UNIT =cursor.getString(3);
            int MEASURE_UNITAGE=cursor.getInt(4) ;
            String LIST_SEQ =cursor.getString(5);
            String DETAIL=cursor.getString(6);
			
			// here create a Place class to add info.
			FOODSList.add(new T_FOODS(_ID, NAME, FOOD_TYPE, MEASURE_UNIT , MEASURE_UNITAGE , LIST_SEQ , DETAIL));

		}
			// here create a Place class to add info.


		cursor.close();

		return FOODSList;
	}

	public ArrayList<MyDataBase_T_FOODS.T_FOODS> SearchT_FOODS(
			String selectWhat, String where_expression) {
		ArrayList<MyDataBase_T_FOODS.T_FOODS> FOODSList = new ArrayList<MyDataBase_T_FOODS.T_FOODS>();
		Cursor cursor = db.rawQuery(String.format(
				"select %s from %s where %s;", selectWhat, TABLENAME,
				where_expression), null);
		while (cursor.moveToNext()) {
			
				int _ID = cursor.getInt(0);
				String NAME = cursor.getString(1);
				int FOOD_TYPE = cursor.getInt(2);
				String MEASURE_UNIT =cursor.getString(3);
	            int MEASURE_UNITAGE=cursor.getInt(4) ;
	            String LIST_SEQ =cursor.getString(5);
	            String DETAIL=cursor.getString(6);
			
	            FOODSList.add(new T_FOODS( _ID, NAME, FOOD_TYPE, MEASURE_UNIT , MEASURE_UNITAGE , LIST_SEQ , DETAIL));

		}
		cursor.close();
		return FOODSList;
	}

	public void Close() {
		this.db.close();
	}

	public class DataBase extends DatabaseHelper {
		public DataBase(Context context) {
			
			super(context);
			// TODO Auto-generated constructor stub
		}

	}

	// here is a Type of Place
	public class T_FOODS {
		
		public int  _ID;
		public String NAME;
		public int  FOOD_TYPE;
		public String MEASURE_UNIT;
		public int  MEASURE_UNITAGE;
		public String LIST_SEQ;
		public String DETAIL;

		

		T_FOODS( int _ID, String NAME, int FOOD_TYPE,String MEASURE_UNIT ,int MEASURE_UNITAGE ,String LIST_SEQ ,String DETAIL ) {
		//T_FOODS(int RecNo,String locale  ) {
			this. _ID= _ID;
			this.NAME=NAME;
			this.FOOD_TYPE=FOOD_TYPE;
			this.MEASURE_UNIT=MEASURE_UNIT;
			this.MEASURE_UNITAGE=MEASURE_UNITAGE;
			this. LIST_SEQ=LIST_SEQ;
			this.DETAIL=DETAIL;

		}

	}

}